#if <%- getApiDefineFlag(api) %>

using PlayFab.<%- api.name %>Models;
using PlayFab.Internal;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace PlayFab
{
<%- generateApiSummary("    ", api, "description")
%>    public class PlayFab<%- api.name %>InstanceAPI
    {
        public readonly PlayFabApiSettings apiSettings = null;
        public readonly PlayFabAuthenticationContext authenticationContext = null;
<% if( api.name === "Admin" || api.name === "Server" ) { %>
        public PlayFab<%- api.name %>InstanceAPI() { }

        public PlayFab<%- api.name %>InstanceAPI(PlayFabApiSettings settings)
        {
            apiSettings = settings;
        }

        public PlayFab<%- api.name %>InstanceAPI(PlayFabAuthenticationContext context)
        {
            authenticationContext = context;
        }

        public PlayFab<%- api.name %>InstanceAPI(PlayFabApiSettings settings, PlayFabAuthenticationContext context)
        {
            apiSettings = settings;
            authenticationContext = context;
        }
<% } else if( api.name === "Client" || api.name === "Authentication" ) { %>
        public PlayFab<%- api.name %>InstanceAPI()
        {
            authenticationContext = new PlayFabAuthenticationContext();
        }

        public PlayFab<%- api.name %>InstanceAPI(PlayFabApiSettings settings)
        {
            apiSettings = settings;
            authenticationContext = new PlayFabAuthenticationContext();
        }

        public PlayFab<%- api.name %>InstanceAPI(PlayFabAuthenticationContext context)
        {
            authenticationContext = context ?? new PlayFabAuthenticationContext();
        }

        public PlayFab<%- api.name %>InstanceAPI(PlayFabApiSettings settings, PlayFabAuthenticationContext context)
        {
            apiSettings = settings;
            authenticationContext = context ?? new PlayFabAuthenticationContext();
        }
<% } else { %>
        public PlayFab<%- api.name %>InstanceAPI(PlayFabAuthenticationContext context)
        {
            if (context == null)
                throw new PlayFabException(PlayFabExceptionCode.AuthContextRequired, "Context cannot be null, create a PlayFabAuthenticationContext for each player in advance, or get <PlayFabClientInstanceAPI>.authenticationContext");
            authenticationContext = context;
        }

        public PlayFab<%- api.name %>InstanceAPI(PlayFabApiSettings settings, PlayFabAuthenticationContext context)
        {
            if (context == null)
                throw new PlayFabException(PlayFabExceptionCode.AuthContextRequired, "Context cannot be null, create a PlayFabAuthenticationContext for each player in advance, or get <PlayFabClientInstanceAPI>.authenticationContext");
            apiSettings = settings;
            authenticationContext = context;
        }
<% } %><% if (hasClientOptions) { %>
        /// <summary>
        /// Verify client login.
        /// </summary>
        public bool IsClientLoggedIn()
        {
            return authenticationContext == null ? false : authenticationContext.IsClientLoggedIn();
        }
<% } %><% if (hasEntityTokenOptions) { %>
        /// <summary>
        /// Verify entity login.
        /// </summary>
        public bool IsEntityLoggedIn()
        {
            return authenticationContext == null ? false : authenticationContext.IsEntityLoggedIn();
        }
<% } %>
        /// <summary>
        /// Clear the Client SessionToken which allows this Client to call API calls requiring login.
        /// A new/fresh login will be required after calling this.
        /// </summary>
        public void ForgetAllCredentials()
        {
            authenticationContext?.ForgetAllCredentials();
        }

<% for(var i in api.calls) { var apiCall = api.calls[i];
%><%- generateApiSummary("        ", apiCall, "summary")
%><%- getDeprecationAttribute("        ", apiCall)
%>        public async Task<PlayFabResult<<%- apiCall.result %>>> <%- apiCall.name %>Async(<%- apiCall.request %> request, object customData = null, Dictionary<string, string> extraHeaders = null)
        {
            await new PlayFabUtil.SynchronizationContextRemover();

            var requestContext = request?.AuthenticationContext ?? authenticationContext;
            var requestSettings = apiSettings ?? PlayFabSettings.staticSettings;
<%- getRequestActions("            ", apiCall, true) %>
            var httpResult = await PlayFabHttp.DoPost("<%- apiCall.url %>", request, <%- getAuthParams(apiCall, true) %>, extraHeaders, requestSettings);
            if (httpResult is PlayFabError)
            {
                var error = (PlayFabError)httpResult;
                PlayFabSettings.GlobalErrorHandler?.Invoke(error);
                return new PlayFabResult<<%- apiCall.result %>> { Error = error, CustomData = customData };
            }

            var resultRawJson = (string)httpResult;
            var resultData = PluginManager.GetPlugin<ISerializerPlugin>(PluginContract.PlayFab_Serializer).DeserializeObject<PlayFabJsonSuccess<<%- apiCall.result %>>>(resultRawJson);
            var result = resultData.data;
<%- getResultActions("            ", apiCall, api, true) %>
            return new PlayFabResult<<%- apiCall.result %>> { Result = result, CustomData = customData };
        }

<% } %><% if (hasClientOptions) {
%>        private async Task<PlayFabResult<AttributeInstallResult>> MultiStepClientLogin(bool needsAttribution)
        {
            var requestSettings = apiSettings ?? PlayFabSettings.staticSettings;
            if (needsAttribution && !requestSettings.DisableAdvertising && !string.IsNullOrEmpty(requestSettings.AdvertisingIdType) && !string.IsNullOrEmpty(requestSettings.AdvertisingIdValue))
            {
                var request = new AttributeInstallRequest();
                if (requestSettings.AdvertisingIdType == PlayFabSettings.AD_TYPE_IDFA)
                    request.Idfa = requestSettings.AdvertisingIdValue;
                else if (requestSettings.AdvertisingIdType == PlayFabSettings.AD_TYPE_ANDROID_ID)
                    request.Adid = requestSettings.AdvertisingIdValue;
                else
                    return null;
                return await AttributeInstallAsync(request);
            }
            return null;
        }
<% } %>    }
}
#endif
